package logic

import (
	"context"
	"goods_srv/goods"
	"goods_srv/internal/svc"
	"goods_srv/models/mysqlinit"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *goods.LoginRequest) (*goods.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var users mysqlinit.User
	mysqlinit.DB.Debug().Where("account = ?", in.Account).Find(&users)
	if users.ID == 0 {
		users = mysqlinit.User{

			Account:  in.Account,
			Password: in.Password,
		}
		mysqlinit.DB.Create(&users)
	}
	return &goods.LoginResponse{
		Id: int64(users.ID),
	}, nil
}
